<template>
  <div>
    <span>维修项目二级列表</span>
    <FilterTable
      ref="table"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :dataSource="dataSource"
      :mainInfo="mainInfo"
      :isSelectColumn="false"
      :onColumnsChange="columns => (this.columns = columns)"
    >
      <template slot="controls" slot-scope="props">
        <el-button size="small" type="primary" icon="el-icon-plus" @click="addHandle">新增</el-button>
        <!-- <el-button size="small">导出</el-button> -->
      </template>
    </FilterTable>
    <BaseDialog :visible.sync="visible" :title="dialogTitle" destroyOnClose width="500px" :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <subGroup :type="handleType" :rowData="formData" @close="closeHandler" />
    </BaseDialog>
  </div>
</template>

<script>
import { mapState } from 'vuex';
import { dictionary } from '@/utils/dictMixin';
import subGroup from './subGroup';
import { getSubGroupList } from '@/api/repair/repair/labourGroup';

export default {
  name: 'Ordermanage',
  components: {
    subGroup
  },
  mixins: [dictionary],
  props: {
    dataSource: Array,
    mainInfo: {}
  },
  data() {
    this.BaseTable = null;
    this.conf = { add: '新增', edit: '编辑', show: '显示' };
    return {
      visible: false,
      fetchApi: getSubGroupList,
      dialogTitle: '',
      fetchParams: { xhrAbort: true },
      columns: this.createTableColumns(),
      handleType: '',
      formData: {}
    };
  },
  watch: {
    dataSource(dataSource) {
      console.log('dataSource watch', dataSource);
    },
    mainInfo(mainInfo) {
      console.log('mainInfo watch', mainInfo);
    }
  },

  mounted() {
    window.asd = this;

    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  methods: {
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 200,
          fixed: 'left',
          render: props => {
            return (
              <div>
                <el-button size="mini" type="text" onClick={() => this.editHandle(props.row)}>
                  编辑
                </el-button>
                <el-divider direction="vertical" />
              </div>
            );
          }
        },

        {
          title: '主分类代码',
          dataIndex: 'mainGroupCode',
          // width: 150,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '主分类名称',
          dataIndex: 'mainGroupName',
          // width: 150,
          sorter: true,
          filter: true,
          filterType: 'input'
        },

        {
          title: '二级分类码',
          dataIndex: 'subGroupCode',
          // width: 150,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '二级分类名称',
          dataIndex: 'subGroupName',
          // width: 150,
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    },

    changeHandle(val) {
      this.fetchParams = Object.assign({}, val, { xhrAbort: false });
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    closeHandler(val, type) {
      this.visible = val;
      if (type === 'refresh') {
        // 重新加载
        this.getSubGroupList(this.mainInfo);
      }
    },
    async getSubGroupList(row) {
      const res = await getSubGroupList(row.mainGroupCode, row);
      this.dataSource = res.rows;
      this.mainInfo = row;
      // this.parmes.list1 = res;
    },
    addHandle() {
      this.handleType = 'add';
      if (this.mainInfo.mainGroupCode != undefined) {
        console.log(this.mainInfo);
        this.formData = this.mainInfo;
        this.showHandler();
      } else {
        this.$notify({
          // title: '警告',
          message: '请双击选择父表格数据',
          type: 'warning'
        });
      }
    },
    editHandle(row) {
      this.handleType = 'edit';
      this.formData = row;
      console.log(row);
      this.showHandler();
    },

    showHandler() {
      this.dialogTitle = `${this.conf[this.handleType]}维修项目二级分类`;
      this.visible = true;
    }
  }
};
</script>
